<?php
    $textToDelete = file_get_contents('listOfExpenses.txt');
    $textToDelete = trim($textToDelete);
    $allRecordsToDelete = explode("\n", $textToDelete);
    if(count($allRecordsToDelete) == 1)
    {
        unlink('listOfExpenses.txt');
    }
    else
    {
        unset($allRecordsToDelete[$_POST['delete']]);
        $allRecordsToDelete = array_values($allRecordsToDelete);
        foreach($allRecordsToDelete as $keyToDelete=>$valueToDelete)
        {
            if ($keyToDelete == 0)
            {
                file_put_contents('listOfExpenses.txt', $allRecordsToDelete[$keyToDelete]."\n");
            } 
            else
            {
                file_put_contents('listOfExpenses.txt', $allRecordsToDelete[$keyToDelete]."\n", FILE_APPEND);
            }
        }
    }



    header('Location: index.php');
?>